# Find microhttpd
#
# Find the microhttpd includes and library
#
# if you need to add a custom library search path, do it via via CMAKE_PREFIX_PATH
#
# This module defines
#  MHD_INCLUDE_DIRS, where to find header, etc.
#  MHD_LIBRARIES, the libraries needed to use jsoncpp.
#  MHD_FOUND, If false, do not try to use jsoncpp.

find_path(
	MHD_INCLUDE_DIR
	NAMES microhttpd.h
	DOC "microhttpd include dir"
)

find_library(
	MHD_LIBRARY
	NAMES microhttpd microhttpd-10 libmicrohttpd libmicrohttpd-dll
	DOC "microhttpd library"
)

set(MHD_INCLUDE_DIRS ${MHD_INCLUDE_DIR})
set(MHD_LIBRARIES ${MHD_LIBRARY})

# debug library on windows
# same naming convention as in QT (appending debug library with d)
# boost is using the same "hack" as us with "optimized" and "debug"
# official MHD project actually uses _d suffix
if (MSVC)

	find_library(
		MHD_LIBRARY_DEBUG
		NAMES microhttpd_d microhttpd-10_d libmicrohttpd_d libmicrohttpd-dll_d
		DOC "mhd debug library"
	)

	set(MHD_LIBRARIES optimized ${MHD_LIBRARIES} debug ${MHD_LIBRARY_DEBUG})

	# prepare dlls
	string(REPLACE ".lib" ".dll" MHD_DLL "${MHD_LIBRARY}")
	string(REPLACE "/lib/" "/bin/" MHD_DLL "${MHD_DLL}")
	string(REPLACE ".lib" ".dll" MHD_DLL_DEBUG "${MHD_LIBRARY_DEBUG}")
	string(REPLACE "/lib/" "/bin/" MHD_DLL_DEBUG "${MHD_DLL_DEBUG}")
	set(MHD_DLLS optimized ${MHD_DLL} debug "${MHD_DLL_DEBUG}")

endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(mhd DEFAULT_MSG
	MHD_LIBRARY MHD_INCLUDE_DIR)

mark_as_advanced(MHD_INCLUDE_DIR MHD_LIBRARY)
# Find microhttpd
#
# Find the microhttpd includes and library
#
# if you need to add a custom library search path, do it via via CMAKE_PREFIX_PATH
#
# This module defines
#  MHD_INCLUDE_DIRS, where to find header, etc.
#  MHD_LIBRARIES, the libraries needed to use jsoncpp.
#  MHD_FOUND, If false, do not try to use jsoncpp.

find_path(
	MHD_INCLUDE_DIR
	NAMES microhttpd.h
	DOC "microhttpd include dir"
)

find_library(
	MHD_LIBRARY
	NAMES microhttpd microhttpd-10 libmicrohttpd libmicrohttpd-dll
	DOC "microhttpd library"
)

set(MHD_INCLUDE_DIRS ${MHD_INCLUDE_DIR})
set(MHD_LIBRARIES ${MHD_LIBRARY})

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(mhd DEFAULT_MSG
	MHD_LIBRARY MHD_INCLUDE_DIR)

#mark_as_advanced(MHD_INCLUDE_DIR MHD_LIBRARY)


message(STATUS "FindMicrohttpd check")

#include(FindPkgConfig)
#if ( PKG_CONFIG_FOUND )
#   pkg_check_modules (PC_MICROHTTPD microhttpd)
#   set(MICROHTTPD_DEFINITIONS ${PC_MICROHTTPD_CFLAGS_OTHER})
##   message(STATUS "micrhttpd checked:" ${MICROHTTPD_DEFINITIONS})
#endif(PKG_CONFIG_FOUND)
# this does nothing

# set defaults
SET(_microhttpd_HOME "/usr/local")
SET(_microhttpd_INCLUDE_SEARCH_DIRS
  ${CMAKE_INCLUDE_PATH}
  /usr/local/include
  /usr/include
  )
SET(_microhttpd_LIBRARIES_SEARCH_DIRS
  ${CMAKE_LIBRARY_PATH}
  /usr/local/lib
  /usr/lib
  )

##
if( "${MICROHTTPD_HOME}" STREQUAL "")
  if("" MATCHES "$ENV{MICROHTTPD_HOME}")
    message(STATUS "MICROHTTPD_HOME env is not set, setting it to /usr/local")
    set (MICROHTTPD_HOME ${_microhttpd_HOME})
  else("" MATCHES "$ENV{MICROHTTPD_HOME}")
    set (MICROHTTPD_HOME "$ENV{MICROHTTPD_HOME}")
  endif("" MATCHES "$ENV{MICROHTTPD_HOME}")
else( "${MICROHTTPD_HOME}" STREQUAL "")
  message(STATUS "MICROHTTPD_HOME is not empty: \"${MICROHTTPD_HOME}\"")
endif( "${MICROHTTPD_HOME}" STREQUAL "")
##

message(STATUS "Looking for microhttpd in ${MICROHTTPD_HOME}")

IF( NOT ${MICROHTTPD_HOME} STREQUAL "" )
    SET(_microhttpd_INCLUDE_SEARCH_DIRS ${MICROHTTPD_HOME}/include ${_microhttpd_INCLUDE_SEARCH_DIRS})
    SET(_microhttpd_LIBRARIES_SEARCH_DIRS ${MICROHTTPD_HOME}/lib ${_microhttpd_LIBRARIES_SEARCH_DIRS})
    SET(_microhttpd_HOME ${MICROHTTPD_HOME})
message(STATUS "setting search dirs")
ENDIF( NOT ${MICROHTTPD_HOME} STREQUAL "" )

IF( NOT $ENV{MICROHTTPD_INCLUDEDIR} STREQUAL "" )
  SET(_microhttpd_INCLUDE_SEARCH_DIRS $ENV{MICROHTTPD_INCLUDEDIR} ${_microhttpd_INCLUDE_SEARCH_DIRS})
ENDIF( NOT $ENV{MICROHTTPD_INCLUDEDIR} STREQUAL "" )

IF( NOT $ENV{MICROHTTPD_LIBRARYDIR} STREQUAL "" )
  SET(_microhttpd_LIBRARIES_SEARCH_DIRS $ENV{MICROHTTPD_LIBRARYDIR} ${_microhttpd_LIBRARIES_SEARCH_DIRS})
ENDIF( NOT $ENV{MICROHTTPD_LIBRARYDIR} STREQUAL "" )

IF( MICROHTTPD_HOME )
  SET(_microhttpd_INCLUDE_SEARCH_DIRS ${MICROHTTPD_HOME}/include ${_microhttpd_INCLUDE_SEARCH_DIRS})
  SET(_microhttpd_LIBRARIES_SEARCH_DIRS ${MICROHTTPD_HOME}/lib ${_microhttpd_LIBRARIES_SEARCH_DIRS})
  SET(_microhttpd_HOME ${MICROHTTPD_HOME})
   message(STATUS "microhttpdhome")
ENDIF( MICROHTTPD_HOME )

# find the include files
FIND_PATH(MICROHTTPD_INCLUDE_DIR microhttpd.h
   HINTS
     ${_microhttpd_INCLUDE_SEARCH_DIRS}
     ${PC_MICROHTTPD_INCLUDEDIR}
     ${PC_MICROHTTPD_INCLUDE_DIRS}
    ${CMAKE_INCLUDE_PATH}
)

IF( NOT $ENV{MICROHTTPD_INCLUDE_DIR} STREQUAL "" )
 message(STATUS, "are we here?")
ENDIF( NOT $ENV{MICROHTTPD_INCLUDE_DIR} STREQUAL "" )

# locate the library

SET(MICROHTTPD_LIBRARY_NAMES ${MICROHTTPD_LIBRARY_NAMES} libmicrohttpd.a)
SET(MICROHTTPD_LIBRARY_NAMES ${MICROHTTPD_LIBRARY_NAMES} libmicrohttpd.so)
FIND_LIBRARY(MICROHTTPD_LIBRARY NAMES ${MICROHTTPD_LIBRARY_NAMES}
  HINTS
    ${_microhttpd_LIBRARIES_SEARCH_DIRS}
    ${PC_MICROHTTPD_LIBDIR}
    ${PC_MICROHTTPD_LIBRARY_DIRS}
)

# if the include and the program are found then we have it
IF(MICROHTTPD_INCLUDE_DIR AND MICROHTTPD_LIBRARY)
  SET(MICROHTTPD_FOUND "YES")
  message(STATUS "microhttpdhome found")
ENDIF(MICROHTTPD_INCLUDE_DIR AND MICROHTTPD_LIBRARY)

#  list(APPEND MICROHTTPD_LIBRARY " -lrt")
#  ????
message(STATUS "microhttpdhome library" ${MICROHTTPD_LIBRARY})

MARK_AS_ADVANCED(
  MICROHTTPD_FOUND
  MICROHTTPD_LIBRARY
  MICROHTTPD_INCLUDE_DIR
)

#add_library(jsonrpc::mhd STATIC IMPORTED)
#set_property(TARGET jsonrpc::mhd PROPERTY IMPORTED_LOCATION ${MICROHTTPD_LIBRARY})

link_libraries(${MICROHTTPD_LIBRARIES})



